<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta content="IE=edge" http-equiv="X-UA-Compatible">
    <meta content="width=device-width, initial-scale=1.0" name="viewport">
    <title>课堂教务系统</title>
    <link href="layui/css/layui.css" rel="stylesheet">
    <link href="css/login.css" rel="stylesheet">
    <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js">
    </script>
    <script src="layui/layui.js"></script>
    <script>
        $.ajax({
            url: '/user/userStatus',
            method: 'get',
            success: function (data) {
                if (data.status) {
                    if (data.userType === '1') {
                        window.location.href = '/pages/Main_1.html';
                    } else if (data.userType === '2') {
                        window.location.href = '/pages/Main_2.html';
                    }
                }
            }
        })
    </script>
</head>


<body>
<div id="center">
    <form class="layui-form" id="login" lay-filter="example">
        <p>登录</p>
        <!--更改username为stuid作为登录凭据，因为username不是唯一的-->
        <input autocomplete="off" class="layui-input" lay-verify="stuid" name="stuid" placeholder="请输入学号"
               type="text">

        <input autocomplete="off" class="layui-input" lay-verify="pass" name="password" placeholder="请输入密码"
               type="password">
        <select name="usertype">
            <option value="-1">请选择身份</option>
            <option value="1">教师</option>
            <option value="2">学生</option>
        </select>
        <button class="layui-btn" id="btn" lay-filter="demo1" lay-submit="">登录</button>
    </form>
</div>
<script>
    layui.use(['form', 'layedit', 'laydate'], function () {
            let form = layui.form,
                layedit = layui.layedit;
            //创建一个编辑器
            layedit.build('LAY_demo_editor');

            //自定义验证规则
            form.verify({
                /*此部分进行了修改！*/
                stuid: function (value) {
                    if (value.length !== 10) {
                        return '请输入10位数的账号!';
                    }
                }
                ,
                pass: [
                    /^[\S]{3,12}$/, '密码必须3到12位，且不能出现空格'
                ]
            });
            //监听提交
            form.on('submit()', function (data) {
                let stuid = data.field.stuid;
                let pwd = data.field.password;
                let type = data.field.usertype;
                if (type !== '-1') {
                    //发送验证请求
                    $.ajax({
                        url: "user/login",
                        method: "POST",
                        data: {"stuid": stuid, "pwd": pwd, "usertype": type},
                        complete: function (XMLHttpRequest) {
                            let type = Number.parseInt(XMLHttpRequest.getResponseHeader("LoginStatus"));
                            switch (type) {
                                case 1 : {
                                    window.location.href = "pages/Main_1.html";
                                    break;
                                }
                                case 2 : {
                                    window.location.href = "pages/Main_2.html";
                                    break;
                                }
                                default: {
                                    alert('用户名或密码不正确');
                                }
                            }
                        }
                    })
                } else {
                    alert('未选择登录身份!')
                }
                return false;//不提交页面
            })
        }
    );
    // //页面加载完成后执行的代码块；通过重定向获取后端传递来的状态码
    // window.onload = () => {
    //
    // }
</script>
</body>

</html>